package com.ctgu.fastleader.controller;

import com.ctgu.fastleader.service.SupplierService;
import com.ctgu.fastleader.vo.Supplier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/supplier")
public class SupplierController {

    @Resource
    private SupplierService supplierService;


    @ResponseBody
    @RequestMapping("/allSupplier")
    public List<Supplier> allSupplier(){
        return supplierService.findAllSupplier();
    }

    @ResponseBody
    @RequestMapping("/searchSupplierByKey")
    public List<Supplier> searchSupplierByKey(Integer supid,String supname,String supphone){
        return supplierService.searchSupplierByKey(supid,supname,supphone);
    }

    @RequestMapping("/addSupplier")
    public String addSupplier(HttpServletRequest request){
        int supid = Integer.parseInt(request.getParameter("supid"));
        String supname = request.getParameter("supname");
        String supphone = request.getParameter("supphone");
        String supad = request.getParameter("supad");

        Supplier supplier = new Supplier(supid,supname,supphone,supad);
        supplierService.insertSelective(supplier);
        return "redirect:/page/toSupplier";
    }

    @ResponseBody
    @RequestMapping("/deleteSupplier")
    public int deleteSupplier(String[] obj){
        int[] id = new int[obj.length];
        for (String array:obj){
            supplierService.deleteByPrimaryKey(Integer.parseInt(array));
        }
        return 0;
    }
}
